package com.csdj.t309tgspringboot.config;

import com.csdj.t309tgspringboot.service.RedisService;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * web相关的配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void addInterceptors(InterceptorRegistry registry, HttpSession session, HttpServletRequest request, HttpServletResponse response, Object handler) {
        try {
            registry.addInterceptor(new LoginInterceptor(stringRedisTemplate,session,request,response,handler))
                    .addPathPatterns("/static/**")//需要拦截的路径
                    .excludePathPatterns("/login");//不需要拦截的路径
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login");
        registry.addViewController("/registerUser");
        registry.addViewController("/index");
        registry.addViewController("/top");
        registry.addViewController("/lief");
        registry.addViewController("/chaxun");
        registry.addViewController("/community");
        registry.addViewController("/fabiaotiezi");
        registry.addViewController("/community-post");
    }
}
